package com.wallet.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.wallet.entity.Meta;
import com.wallet.entity.Route;
import com.wallet.entity.example.MetaExample;
import com.wallet.entity.form.MetaForm;
import com.wallet.entity.form.RolesForm;
import com.wallet.entity.form.RoutesForm;
import com.wallet.entity.result.JsonResult;
import com.wallet.entity.result.ResultCode;
import com.wallet.service.BaseService;
import com.wallet.service.Impl.RouteServiceImpl;
import com.wallet.util.HttpClientUtil;

@RestController
@RequestMapping("/routes")
public class RouteController {

	@Autowired
	private RouteServiceImpl routeService;
	@Autowired
	private BaseService<Meta, MetaExample> metaService;
		
	private static List<RoutesForm> buildTree(List<RoutesForm> listRoutesForm,int pid){
		List<RoutesForm> treeList=new ArrayList<RoutesForm>();
		for (RoutesForm routesForm : listRoutesForm) {
			if(routesForm.getPid()==pid) {
				routesForm.setChildren(buildTree(listRoutesForm, routesForm.getId()));
				treeList.add(routesForm);
			}
		}
		return treeList;
	}
	
	@ResponseBody
	@RequestMapping(value = "/show")
	public JsonResult getRoutes() {
		JsonResult result=new JsonResult();
		List<RoutesForm> list=routeService.selectRouteShow();
		List<RoutesForm> listEnd=buildTree(list, 0);
		result.setCode(ResultCode.SUCCESS.getVal());
		result.setData(listEnd);
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/init")
	public String init() {
		String result=HttpClientUtil.get("http://localhost:9527/dev-api/vue-element-admin/roles");
		Map<String, Object> map=(Map<String,Object>)JSON.parse(result);
		String data=map.get("data").toString();
		List<RolesForm> roleForms=JSON.parseArray(data, RolesForm.class);
		List<RoutesForm> routesForms=roleForms.get(0).getRoutes();
		for (RoutesForm routesForm : routesForms) {
			showChildren(routesForm, 0);
		}
		List<RoutesForm> routesForms1=roleForms.get(1).getRoutes();
		for (RoutesForm routesForm : routesForms1) {
			showChildren(routesForm, 0);
		}
		List<RoutesForm> routesForms2=roleForms.get(2).getRoutes();
		for (RoutesForm routesForm : routesForms2) {
			showChildren(routesForm, 0);
		}
		return data;
	}
	
	public void showChildren(RoutesForm routesForm,Integer pid){
		List<RoutesForm>list=routesForm.getChildren();
		routesForm.setChildren(null);
		Route route=saveRoutesForm(routesForm, pid);
		if(list!=null && route!=null) {
			for (int i = 0; i < list.size(); i++) {
				showChildren(list.get(i),route.getId());
			}
		}
	}
	
	private Route saveRoutesForm(RoutesForm routesForm,Integer pid){
		Route route=new Route();
		route.setAlwaysshow(routesForm.getAlwaysShow());
		route.setComponent(routesForm.getComponent());
		route.setName(routesForm.getName());
		route.setPath(routesForm.getPath());
		route.setPid(pid);
		route.setRedirect(routesForm.getRedirect());
		route.setHidden(routesForm.getHidden());
		Route routesel=routeService.selectRouteByAll(route);
		if(routesel==null) {
			routeService.save(route);
			if (routesForm.getMeta()!=null) {
				MetaForm metaForm=routesForm.getMeta();
				Meta meta=new Meta();
				meta.setAffix(metaForm.getAffix());
				meta.setIcon(metaForm.getIcon());
				meta.setNocache(metaForm.getNocache());
				meta.setTitle(metaForm.getTitle());
				meta.setRouteId(route.getId());
				if(metaForm.getRoles()!=null) {
					String []roles=metaForm.getRoles();
					StringBuilder mateData=new StringBuilder();
					mateData.append(roles[0]);
					if(roles.length>1) {
						for (int i = 1; i < roles.length; i++) {
							mateData.append(","+roles[i]);
						}
					}
					meta.setRoles(mateData.toString());
				}
				metaService.save(meta);
			}
			return route;
		}else {
			return null;
		}
		
		
	}
}
